package com.dht.xiao.jianye.activity;

import android.os.Bundle;
import android.util.Log;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.base.BaseActivity;
import com.dht.xiao.jianye.base.BaseBean;
import com.dht.xiao.jianye.bean.OrderDaoBean;
import com.dht.xiao.jianye.http.MainHttp;
import com.dht.xiao.jianye.utils.DBManager;
import com.dht.xiao.jianye.utils.Information;
import com.dlc.video.ADView;
import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AdvertisingActivity extends BaseActivity {
    @BindView(R.id.adview)
    ADView mAdview;
    private boolean unFirst;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_advertsing;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mAdview.setmADViewClickListener(position -> startActivity(MainActivity.class));
        init();
    }

    private void init() {

        List<String> imgUrl = new ArrayList<>();
        List<String> adUrl = new ArrayList<>();
//        imgUrl.add("http://192.168.0.123:8080/profile/2019/09/03/8eae49ec15d8a87941e048036a028c20.png");
//        imgUrl.add("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
        if (Information.facilityinfo != null&&Information.facilityinfo.data != null&&
                Information.facilityinfo.data.slides != null) {
            for (int i = 0; i < Information.facilityinfo.data.slides.size(); i++) {
                imgUrl.add(Information.facilityinfo.data.slides.get(i).slideImg);
            }
        }
        mAdview.setImgList(imgUrl);
        mAdview.setVideoList(adUrl);
        mAdview.start();
        unFirst = true;


    }

    Disposable mDisposable;

    /**
     * 查询数据库是否有未提交数据
     */
    private void startQueryDb(int time){
        Observable.timer(time, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        List<OrderDaoBean>  list =  DBManager.getInstance(AdvertisingActivity.this).query(0);

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }




    @Override
    protected void onResume() {
        super.onResume();
        Log.e("))))))))))", unFirst + "======unFirst");
        if (unFirst) {
            mAdview.resume();
        }

    }



    @OnClick(R.id.adview)
    public void onClick() {
        AdvertisingActivity.this.setResult(MainActivity.AdvertisingResultCode, null);
        AdvertisingActivity.this.finish();
    }
}
